package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id="2021000122669246";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC5UFZLVC1XfV+qD3gTo3piwNUf9wlm6Q2x/bKIalsXLZDnMpTh9xtnZMSw23mXcBZVZFgZP84xbnOTUcPtAnOBRds9hxfkdQADOXdt9v36u0pSj0YnU1TtA79oO0PNW7l4JxIGE/EKBkOZ2hQO3s/RdQKeSgGRQ5S54rw2Fxdv/t0oepdD+bEZMLT53zodAuTNHnbBvRuEqPnUadc2nwnL13aXpIrNDpyQdNKyfgYSFNpTTXgheDoaXnp9BxoO4fpSNjGn+ssKn08TuwHGd5n3tZKJGNXrpIwrrGPTo2uewsLxelWlQjUopl1unzT6c+djJuf9fzh+QjXBaQpISkm5AgMBAAECggEAJA4UbB2ZoM8c4MqXs0J4WmRoUwrOgn5PktToepAu3uiXq9P0ka5xX4GEn2BkXOmK/txCI899P7Lcu8oJ08BJVtqPmH4DUQjOktIIgZWvvqvX/w4vORGGkYLMJ+x3+amiAVWgtRwGJswnNPTwOCBz0K3yb3e4rFm4wx3xPpjpj3lyUTOCPHgYEUm+ei4M39SwdCTRzr9zIZr+F0jsgWBDFfRFS92AJxxOtD+dhtOvhC9EF8gAvNgs5RCmUiAq3MBJtf+TtRUzl04DDs0hBTIGhg9unpUAELjZcBvJ98bAg9xASzkimBYuoGgnx/HQ0toNAhULwSfRVSwc0sL4wn9ATQKBgQD7O8ZC+iLWy1DN3rmqUQQUZDHBMRMC7xKJxql7h1l+9teeluVThUNMMFoJNeq+/MFK89qRPA72RjlDsp7vYI/yZ20T1yhpdUBMwHP+ZuaRdbQYIYEqt2jZZc9HaNv8kSp5BewSz9wFfLfvmpYyO4iLKwdqi7n3PaK91rSY/w6MzwKBgQC81GUTveWeWNciRiksqOPDzhD9NT/U53VltWSSKs8o7tTGHJj9Pv3FVP7nohg9Y4e20qsBnxqRpEqRvkpu4jdRxi4CMmCIKTmeYu7/9CeI9TUpky/bwWUxqNP5eNalbdP0Ph+Kdp7t3jAoKNkSZG99JIG/2sf0mWTS8qOq+kAy9wKBgQCzWDhwF6BHNVy5mgVwT+VZqSzbfOzVa1UQhYD3bee1ewDxdUbAGKGyAdJEQPkS3dweZXXht732kKrQaPKMb6JSqQJ052Mx5UHBiKFFOq/dpuipUUGj/MocZ1FQ6Mohwn9PZargwwGN6TplETWnJliTAGSK6rQWiylh26OQlIMOgQKBgQCPKtYr/qmOwjrywJFNTAvMlHtrD6mC6a6n270R2aVRmqr2Pg58y/qqWAQgaVTJjqICKn3/b7s8O31SPR3ALoHU3VZa0glAk0kdXlNo+pvTCl8xU2hQzLPF0ARPvJ+5s6ilIUjWuZNMNE780+OpPz8MedZqOR2rxXWdPtx6sNxmBwKBgDnWvyybOYYE9TWHqNG0Y0QaYZOdK/xMDaxdr5kPiQ1tCn96LW8KSlaEQOX/f+znLVq/C3JCXE3ZoxY6oeb3bzIBY0HAOsMsA4G8F/lJhUonZIM7djGTEKiDdzqQs6QgUSaFibz1xBssm+tSlsmU27K0yZsClGUZqUIx9pEO5HN6";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlu0HvZ0jzUhrMA4DWdYClSsOjSg+p+N7N56W9fIixN/wOqLRU+5Gplp2NWHv1affqTxOYKp1DGQVxmFoS1fdrSZlIHMN2PmDj1QR8pLzJWKMGbUA5N2/3LZUPvH0YXvPWnPoj+5E+aLEEtPdp5mLWbNj9nM/sf2Xwt2RWJcshwt8l3/caTFmOpNKPdhQRAuvNg5sLANp4gz4A/xP0LMrzWEHFHVG88v52J5OSfiHCcXlZ8ePyfUvSpqRsyjhTms1Z+osZ1jXS+DwbxKol08zKLOVfhflzDmypuY5UqbI1Y6sRweDNJij8v7tsvJdzdWbicG0Jo7yhgFPy34CmA6HfQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type="RSA2";

    // 字符编码格式
    private  String charset;

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl="https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
